package com.yc.AdminUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.utils.Values;
import com.yc.UI.WelcomeUI;
import com.yc.utils.SWTUtil;

import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.ControlListener;
import java.util.function.Consumer;

/**
 *主界面
 * @author UNO
 * 2021年6月22日下午7:25:21
 */
public class AdminMainUI {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AdminMainUI window = new AdminMainUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(1074, 731);
		shell.setText("12306");
		// 设置窗口居中
		SWTUtil.centerWindom(shell);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(shell, SWT.NONE);	
		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);

		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(SWTResourceManager.getImage(AdminMainUI.class, "/images/titleback.png"));
		//图片自适应
		SWTUtil.imagesSize(composite);

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBackgroundImage(SWTResourceManager.getImage(AdminMainUI.class, "/images/title.png"));
		lblNewLabel.setBounds(10, 10, 130, 80);

		Label lblNewLabel_2 = new Label(composite, SWT.CENTER);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.NORMAL));
		lblNewLabel_2.setBounds(271, 38, 642, 33);
		lblNewLabel_2.setText("尊敬的管理员 " + Values.currentLoginUser.get("a_name").toString() + " 欢迎使用12306后台");
		//标签居中未完成
		//SWTUtil.centerLabel(composite, lblNewLabel_2);

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setBackgroundImage(SWTResourceManager.getImage(AdminMainUI.class, "/images/背景.jpg"));
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm_1 = new SashForm(composite_1, SWT.NONE);

		Composite composite_2 = new Composite(sashForm_1, SWT.BORDER);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Button button1 = new Button(composite_2, SWT.CENTER);		
		button1.setBounds(20, 62, 98, 30);
		button1.setText("查询订单");

		Button button2 = new Button(composite_2, SWT.CENTER);
		button2.setBounds(20, 162, 98, 30);
		button2.setText("添加修改列车");

		Button button3 = new Button(composite_2, SWT.CENTER);
		button3.setBounds(20, 362, 98, 30);
		button3.setText("用户管理");

		Button button4 = new Button(composite_2, SWT.CENTER);
		button4.setBounds(20, 262, 98, 30);
		button4.setText("修改座位");

		Composite composite_3 = new Composite(sashForm_1, SWT.BORDER);
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_3.setLayout(Values.stackLayout);

		//图片自适应
//		SWTUtil.imagesSize(composite_3);
		sashForm_1.setWeights(new int[] {147, 895});

		Composite composite_4 = new Composite(sashForm, SWT.NONE);

		Button button_3 = new Button(composite_4, SWT.CENTER);
		button_3.setBounds(742, 3, 98, 30);
		button_3.setText("退出登陆");

		Button button_4 = new Button(composite_4, SWT.NONE);
		button_4.setBounds(918, 3, 98, 30);
		button_4.setText("注销账号");

		Label label = new Label(composite_4, SWT.NONE);
		label.setBounds(10, 10, 186, 20);
		label.setText("联系客服：15789895473");

		Label label_1 = new Label(composite_4, SWT.NONE);
		label_1.setBounds(243, 10, 219, 20);

		label_1.setText("地址：衡阳市红湘街道南华大学");
		sashForm.setWeights(new int[] {102, 490, 36});
		//面板宽度固定的方法
		sashForm.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				sashForm.setWeights(new int[] {105, sashForm.getSize().y - 190, 34});
			}
		});
		sashForm_1.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				sashForm_1.setWeights(new int[] {90, sashForm_1.getSize().y});
			}
		});
		
		
		//默认副界面是个人中心
		Values.adminInfo = new FindOrder(composite_3,SWT.NONE);
		Values.stackLayout.topControl = Values.adminInfo;
		
		//切换副界面
		button1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
			
				Composite topControl = Values.adminInfo;
				Values.adminInfo = new FindOrder(composite_3, SWT.NONE);
				topControl = Values.adminInfo;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}			
		});
		//添加列车
		button2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.addCar;
				Values.addCar = new AddCar(composite_3, SWT.NONE);
				topControl = Values.addCar;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}
		});
		//列车管理
		button3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.carControl;
				Values.carControl = new UserControl(composite_3, SWT.NONE);
				topControl = Values.carControl;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}
		});
		//用户管理
		button4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.userControl;
				Values.userControl = new TicketingControl(composite_3, SWT.NONE);
				topControl = Values.userControl;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}
			
		});
		//退出登录
		button_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				AdminLoginUI ul = new AdminLoginUI();
				shell.dispose();
				ul.open();				
			}
		});
		
	}
}


